<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons')
import resize from './mixins/resize'
import { getToken } from '@/utils/auth'
import { getRouteSection } from '@/api/charts'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      const emphasisStyle = {
        itemStyle: {
          shadowBlur: 10,
          shadowColor: 'rgba(0,0,0,0.3)'
        }
      }
      getRouteSection(getToken(), 1).then(response => {
        this.chart.setOption({
          title: {
            text: '断面客流',
            left: 'center'
          },
          legend: {
            data: ['in', 'out'],
            left: 'left'
          },
          tooltip: {},
          xAxis: {
            data: response.station,
            name: 'station',
            axisLine: { onZero: true },
            splitLine: { show: false },
            splitArea: { show: false }
          },
          yAxis: {},
          grid: {
            bottom: 100
          },
          series: [
            {
              name: 'in',
              type: 'bar',
              stack: 'one',
              emphasis: emphasisStyle,
              data: response.in_list
            },

            {
              name: 'out',
              type: 'bar',
              stack: 'two',
              emphasis: emphasisStyle,
              data: response.out_list
            }
          ]
        })
      })
    }
  }
}
</script>
